﻿using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

public partial class ConfigMessages : Form
{
    public ConfigMessages()
    {
        InitializeComponent();
        this.Icon = new System.Drawing.Icon(this.GetType().Assembly.GetManifestResourceStream("MatrixScreen.MatrixShow.ico"));
    }

    public static string[] defaultMsgs = new string[]{ "The Matrix has you...", "Wake up, Neo...", "Knock, knock, Neo.", "Follow the white rabbit." };

    private void ConfigForm_Load(object sender, System.EventArgs e)
    {
        RegistryKey settings = Registry.CurrentUser.OpenSubKey("Software\\MatrixShow");

        if (settings != null)
        {
            string[] msgs;
            msgs = (string[])settings.GetValue("messages", defaultMsgs);

            if(msgs.Length == 0)
                msgs = defaultMsgs;

            foreach (string msg in msgs)
            {
                lstMsgs.Items.Add(msg);
            }
        }
        else
            SetDefaults(false);

    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtMessage.Text.Trim() != "")
        {
            lstMsgs.Items.Add(txtMessage.Text);
            txtMessage.Text = "";
        }
        else
            MessageBox.Show("Please enter a message in..", "Eh?");
    }

    private void btnRemove_Click(object sender, EventArgs e)
    {
        for (int i = lstMsgs.SelectedItems.Count - 1; i > -1; i--)
        {
            lstMsgs.Items.Remove(lstMsgs.SelectedItems[i]);
        }
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        RegistryKey settings = Registry.CurrentUser.OpenSubKey("Software\\MatrixShow", true);

        if (settings == null)
            settings = Registry.CurrentUser.CreateSubKey("Software\\MatrixShow");
                
        string[] msgs = new string[lstMsgs.Items.Count];

        for (int i = 0; i < lstMsgs.Items.Count; i++)
        {
            msgs[i] = (string)lstMsgs.Items[i];
        }

        settings.SetValue("messages", msgs, RegistryValueKind.MultiString);

        this.Close();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnDefaults_Click(object sender, EventArgs e)
    {
        SetDefaults(true);
    }

    private void SetDefaults(bool prompt)
    {
        if (prompt)
        {
            if (MessageBox.Show("Reset to default settings?", "Reset?", MessageBoxButtons.YesNo) == DialogResult.No)
                return;
        }

        lstMsgs.Items.Clear();

        foreach (string msg in defaultMsgs)
        {
            lstMsgs.Items.Add(msg);
        }
    }

    private void lstMsgs_DoubleClick(object sender, EventArgs e)
    {
        if (lstMsgs.SelectedIndex != -1)
        {
            txtMessage.Text = lstMsgs.Items[lstMsgs.SelectedIndex] as string;
            lstMsgs.Items.RemoveAt(lstMsgs.SelectedIndex);
        }
    }

    private void btnEdit_Click(object sender, EventArgs e)
    {
        lstMsgs_DoubleClick(sender, e);
    }
}

